import Vue from 'vue'
import VueRouter from "vue-router";
import { GetCookie } from '@/utils/jscookie'

Vue.use(VueRouter)

const routes = [
    // 登录验证
    {
        path: '/auth/login',
        name: 'login',
        meta: "",
        component: () => import('@/pages/auth/login')
    },
    {
        path: '/auth/register',
        name: 'register',
        component: () => import('@/pages/auth/register')
    },
    {
        path: '/auth/forget-pw',
        name: 'forget-pw',
        component: () => import('@/pages/auth/forgetPassword')
    },
    {
        path: "/",
        name: "layout",
        component: ()  => import('@/pages/layout'),
        children: [
            // 首页
            {
                path: '/home/index',
                name: 'home',
                component: () => import('@/pages/home/index')
            },
            // 好友列表
            {
                path: '/home/friends',
                name: 'friends',
                component: () => import('@/pages/home/friends')
            },
            // 好友搜索列表
            {
                path: '/home/search',
                name: 'search',
                component: () => import('@/pages/home/searchFriend')
            },
            // 聊天列表
            {
                path: '/home/chat',
                name: 'chat',
                component: () => import('@/pages/home/chat')
            }
        ]
    },
]

var router = new VueRouter({
    'mode': 'hash',
    routes
})

router.beforeEach((to, from, next) => {
    // var token = localStorage.getItem('_token')
    var token = GetCookie('_token')
    if (to.name !== "login" && !token) next({path:"/auth/login"})

    next()
})

export default router